<?php
defined('IN_MAIN') or exit('Invalid Access!');

function build_cat_display($parent_id, $section=3){
 $subcats=get_subcat_by_parent_id($parent_id);
 $t=count($subcats);
 $item_per_sec=ceil($t/$section);
 for($i=0;$i<$t;$i++){
  $subcat_link.="<a href='$this_file?cid=".$subcats[$i]["id"]."'>".$subcats[$i]["title"]."</a>\n";
  if(!(($i+1)%$item_per_sec)){
   $subcat.=
   "<div class='subcat'>
   $subcat_link
   </div>";
   $subcat_link="";
  }
 }
 if($subcat_link){
   $subcat.=
   "<div class='subcat'>
   $subcat_link
   </div>";
   $subcat_link="";
 }

 return $subcat;
}

function get_all_parent_cat($catid){
 global $vars;
 $db=$vars["db"];
 if(@mysql_num_rows($r=mysql_query("select * from $db->product_category where id='$catid'"))){
  $pid=mysql_result($r, 0, "parent_id");
  $i=0;
  while($pid){
   $r=mysql_query("select * from $db->product_category where id='$pid'");
   if(@mysql_num_rows($r)){
    $parents[$i]=mysql_fetch_assoc($r);
    $pid=$parents[$i]["parent_id"];
    $i++;
   }
  }
 }
 return $parents? array_reverse($parents): $parents;
}

function get_subcat_by_parent_id($cat_id){
 global $vars;
 $db=$vars["db"];
 $r=mysql_query($s="select * from $db->product_category where parent_id='$cat_id'");
 if(@mysql_num_rows($r)){
  for($i=0,$t=mysql_num_rows($r);$i<$t;$i++){
   $cats[$i]=mysql_fetch_assoc($r);
  }
 }
 return $cats;
}
?>